package com.anuprayog.flyinghelicopter.model;

import com.anuprayog.flyinghelicopter.model.components.Hspeed;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.MotionEvent;

/**
 * Bag
 * 
 * @author amit
 */
public class DhoniScroll {

   private Bitmap bitmap;  // the actual bitmap
   private int x;          // the X coordinate
   private int y;          // the Y coordinate
   private boolean touched;    // if droid is touched/picked up
   private Hspeed hspeed;	// the speed with its directions
	 
   public DhoniScroll(Bitmap bitmap, int x, int y) {
       this.bitmap = bitmap;
       this.x = x;
       this.y = y;
       this.hspeed = new Hspeed();
   }
    
   public Bitmap getBitmap() {
       return bitmap;
   }
   public void setBitmap(Bitmap bitmap) {
       this.bitmap = bitmap;
   }
   public int getX() {
       return x;
   }
   public void setX(int x) {
       this.x = x;
   }
   public int getY() {
       return y;
   }
   public void setY(int y) {
       this.y = y;
   }

   public boolean isTouched() {
       return touched;
   }

   public void setTouched(boolean touched) {
       this.touched = touched;
   }
   
   public Hspeed getHspeed() {
	   return hspeed;
   }
	
   public void setHspeed(Hspeed hspeed) {
	   this.hspeed = hspeed;
   }

   public void draw(Canvas canvas) {
       //canvas.drawBitmap(bitmap, x - (bitmap.getWidth() / 2), y - (bitmap.getHeight() / 2), null);
       //canvas.drawBitmap(bitmap, null, new Rect(x - (bitmap.getWidth() / 2), y - (bitmap.getHeight() / 2),x - (bitmap.getWidth() / 2) + canvas.getWidth()/8, y - (bitmap.getHeight() / 2) + canvas.getHeight()/12), null);
       canvas.drawBitmap(bitmap, null, new Rect(x - (canvas.getWidth() / 36), y - (canvas.getHeight() / 24),x + (canvas.getWidth() / 36), y + (canvas.getHeight() / 24)), null);
   }
   
   /**
	 * Method which updates the droid's internal state every tick
	 */
	public void update() {
		if (!touched) {
			x += (hspeed.getXv() * hspeed.getxDirection()); 
			y += (hspeed.getYv() * hspeed.getyDirection());
		}
	}

	/**
	 * Handles the {@link MotionEvent.ACTION_DOWN} event. If the event happens on the 
	 * bitmap surface then the touched state is set to <code>true</code> otherwise to <code>false</code>
	 * @param eventX - the event's X coordinate
	 * @param eventY - the event's Y coordinate
	 */
   public void handleActionDown(int eventX, int eventY) {
       if (eventX >= (x - bitmap.getWidth()) && (eventX <= (x + bitmap.getWidth()))) {
           if (eventY >= (y - bitmap.getHeight()) && (eventY <= (y + bitmap.getHeight()))) {
               // droid touched
               setTouched(true);
           } else {
               setTouched(false);
           }
       } else {
           setTouched(false);
       }

   }
}
